<?php

namespace app\admin\model;

/**
 *  矿机视频模型
 */
class MinerVideos extends BaseModel
{
    protected $attributes = [
        'showstate' => [
            0 => '不对外显示',
            1 => '对外显示'
        ],
        'category' => [
            1 => '第三方视频',
            2 => 'JM视频',
            3 => '教程视频',
        ]
    ];


    /**
     *  获取字段枚举
     */
    protected $enumCallbacks = [
        'operator' => 'getOperatorEnum',
        'model' => 'getModelEnum'
    ];

    /**
     *  获取字段枚举
     */
    public function getFieldEnum($field)
    {
        return $this->getEnum($field, [], 'label');
    }

    /**
     *  获取创始人枚举
     */
    public function getOperatorEnum()
    {
        return Operator::order('cname ASC')
            ->column('id as value,cname as label');
    }

    /**
     *  获取机型枚举
     */
    public function getModelEnum()
    {
        return ModelInfo::order('model_name ASC')
            ->column('id as value,model_name as label');
    }

    /**
     *  获取机型名称
     */
    public function modelName()
    {
        return $this->hasOne(ModelInfo::class, 'id', 'model_id');
    }


    /**
     *  获取创建人
     */
    public function creatorName()
    {
        return $this->hasOne(Operator::class, 'id', 'creator');
    }

    /**
     *  获取封面图片
     */
    public function coverimage()
    {
        return $this->hasOne(Images::class, 'id', 'coverimage_id');
    }

    /**
     *  获取视频
     */
    public function video()
    {
        return $this->hasOne(Images::class, 'id', 'video_id');
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2025-04-08 13:12:16 
     * @param string $videoIds
     * @return array
     */
    public function getVideoByIds(string $videoIds)
    {
        $data = $this
            ->whereIn('id', $videoIds)
            ->with(['video' => function ($query) {
                $query->field('id,url,name,alt');
            }])
            ->where('is_del', 0)
            ->field('id,video_title as name,youtube_vid,video_id,coverimage_id')
            ->select()
            ->toArray();
        $coverimage_id = array_column($data, 'coverimage_id');
        $coverimage_id = array_filter($coverimage_id);
        $coverimage_id = array_unique($coverimage_id);
        $imagesModel = new Images();
        $coverimage = $imagesModel->getImagesByIds($coverimage_id);
        $coverimage = array_column($coverimage, null, 'id');

        foreach ($data as $key => &$value) {
            if ($value['video_id']) {
                $value['url'] = $value['video']['url'];
            } else {
                $value['url'] = 'https://www.youtube.com/watch?v=' . $value['youtube_vid'];
            }
            if ($value['coverimage_id']) {
                $value['coverimage'] = $coverimage[$value['coverimage_id']]['url'];
            } else {
                $value['coverimage'] = "https://cdn.minetheasic.com/vi/{$value['youtube_vid']}/maxresdefault.jpg?x-oss-process=image/resize,m_lfit,w_500,limit_0/quality,q_100";
            }
            unset($value['video']);
        }
        return $data;
    }
}
